// 引入JSAPI接口
/*eslint-disable*/
 

let doOther = { result: '1', msg: '需要签署匹配或者不匹配协议'}; // 前面流程成功，需要跳转协议页面,
let stop = { result: '-1' }; // 流程终止，不继续执行下一步
let finish = { result: '0' }; // 流程完成 (有可能是中间任意一步结束)
let continueDo = { result: '2' }; // 流程继续
let waiteDo = { result: '3' }; // 流程等待
 
window.checkInsideEligibility = checkInsideEligibility;
window.checkESignAuth = checkESignAuth;
window.checkPublicFund = checkPublicFund;


export async function checkInsideEligibility(params, business_kind ) {
  return new Promise((resolve, reject) => {

    let count = 0;
    const intervalId = setInterval(() => {
      count++;
      console.log('checkInsideEligibility finish', window.start_elig_biz_id);
      if (count >= 5) {
        clearInterval(intervalId);
        resolve(finish);
      }
    }, 300);
  });
}
  

export async function checkESignAuth(params, business_kind) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      console.log('checkESignAuth finish', params, business_kind);
      resolve(finish);
    }, 3000);
  });
}
 
 
export async function checkPublicFund(params, business_kind) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      console.log('checkPublicFund finish', params, business_kind);
      resolve(finish);
    }, 3000);
  });
}
  

/**
 * 所有适当性流程支持可配置
 * @param searchRes
 * @param params
 * @returns {Promise<void>}
 */
export async function checkAllElig(params, business_kind, successCallback, failCallback){
  startEligProcess(business_kind);
  console.log('===============start function 11111 ======', window.start_elig_biz_id);
  console.log("checkAllElig的params入参===", JSON.stringify(params), business_kind);
  let resultObj = {
    instr_batch_no: "",
    external_check_no:"",
    convert_bond_risk_entrust_confirm_tip:"",
    extend: {}
  }


  let functionList = 'checkInsideEligibility,checkPublicFund,checkESignAuth';
  if(!(functionList.trim())){
    successCallback && successCallback(resultObj);
    return;
  }
   let funcArray = functionList.split(",");
  
  const elementCount = {}; // 用于存储元素出现次数的对象  


  for(let i=0;i< funcArray.length; i++ ){
     let funcName = funcArray[i].trim();

    // 如果元素已经存在于elementCount对象中，则增加其计数  
    if (elementCount[funcName]) {  
      elementCount[funcName]++;  
    } else {  
      // 否则，初始化其计数为1  
      elementCount[funcName] = 1;  
    }  

    // 计算方法第几次出现
    const timeNo = elementCount[funcName]
    console.log('===============start function ======',funcName, window.start_elig_biz_id);

    let res = await window[funcName](params, business_kind, timeNo);
    if(res.result != '0' || checkShouldStopEligProcess(business_kind)){
      failCallback && failCallback();
      return ;
    }else {
      if(funcName == "checkInsideEligibility"){
        resultObj.instr_batch_no = res.instr_batch_no;
      }
      if(funcName == "checkPublicFund"){
        resultObj.external_check_no = res.external_check_no;
      }
      if(funcName == "convertBondRiskTips"){
        resultObj.convert_bond_risk_entrust_confirm_tip = res.convert_bond_risk_entrust_confirm_tip;
      }
      if(res.extend){
        resultObj.extend = res.extend;
      }
    }
  }
  // console.log("resultObj===" + JSON.stringify(resultObj));
  console.log('====checkAllElig====', business_kind);
  successCallback && successCallback(resultObj);
  endEligProcess();

}

/**
 * 开始适当性流程的业务小类
 * @param {*} business_kind 
 */
export function startEligProcess(business_kind){
  window.start_elig_biz_id = business_kind;
  console.log('------------------ start process --------------', window.start_elig_biz_id);
}
/**
 * 结束适当性流程的业务小类，清空当前执行的业务小类（页面消失，tab切换，以及适当性流程结束的时候调用）
 */
export function endEligProcess(){
  console.log('------------------ end process --------------', window.start_elig_biz_id);
  window.start_elig_biz_id = '';
}

/**
 * 校验该适当性是否应该结束，流程中方法发包前/回包到上层业务前/协议弹框前/弹窗报错前 调用
 */
export function checkShouldStopEligProcess(business_kind){
  // 如果当前执行的业务小类和传入的业务小类不一致/当前执行的业务小类为空，需要停止该适当性流程
  if(!window.start_elig_biz_id || window.start_elig_biz_id != business_kind){
    console.log('------------------ check process should stop--------------', business_kind, window.start_elig_biz_id);
    return true;
  }
  return false;
}